package com.db.utils;
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
import java.sql.SQLException;  
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker;  
  


  
public class DbPoolConnection {  
  
    private static DbPoolConnection databasePool = null;  
    private static DruidDataSource dds = null;  
    static {  
    	try {
        Properties properties = loadPropertyFile("db_server.properties");  
          
            dds = (DruidDataSource) DruidDataSourceFactory  
                    .createDataSource(properties);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    private DbPoolConnection() {  
    }  
  
    public static synchronized DbPoolConnection getInstance() {  
        if (null == databasePool) {  
            databasePool = new DbPoolConnection();  
            MySqlValidConnectionChecker checker = new MySqlValidConnectionChecker();
        }  
        return databasePool;  
    }  
  
    public DruidDataSource getDataSource() throws SQLException {  
        return dds;  
    }  
  
    public DruidPooledConnection getConnection() throws SQLException {  
        return dds.getConnection();  
    }  
  
    public static Properties loadPropertyFile(String fullFile) {  
        String webRootPath = null;  
        if (null == fullFile || fullFile.equals(""))  
            throw new IllegalArgumentException(  
                    "Properties file path can not be null : " + fullFile);  
        webRootPath = DbPoolConnection.class.getClassLoader().getResource("\\")  
                .getPath();  
        webRootPath = new File(webRootPath).getParent();
        System.out.println("1:"+DbPoolConnection.class.getClassLoader());
        System.out.println("2:"+DbPoolConnection.class.getClassLoader().getResource("\\"));
        System.out.println("webRootPath: "+webRootPath);
        InputStream inputStream = null;  
        Properties p = null;  
        try {  
            String profilepath = webRootPath + File.separator + fullFile;  
            System.out.println("======"+profilepath);  
            inputStream = new FileInputStream(new File(profilepath));  
            p = new Properties();  
            p.load(inputStream);  
        } catch (FileNotFoundException e) {  
            throw new IllegalArgumentException("Properties file not found: "  
                    + fullFile);  
        } catch (IOException e) {  
            throw new IllegalArgumentException(  
                    "Properties file can not be loading: " + fullFile);  
        } finally {  
            try {  
                if (inputStream != null)  
                    inputStream.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return p;  
    }  
  
}  